package com.zzhou.linkinfo.clock.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.zzhou.linkinfo.clock.R;
import com.zzhou.linkinfo.clock.adapter.ArrayWheelAdapter;
import com.zzhou.linkinfo.clock.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布打卡任务周期选择弹框
 * Created by Carlos on 2019/03/16
 */
public class ClockReleaseCycleSelectDialog extends Dialog {
    private Context mContext;
    private IOnClickListener mOnClickListener;
    private String mSelectedCycle = "1";
    private int mCurrentItem;

    public ClockReleaseCycleSelectDialog(@NonNull Context context, int currentItem, IOnClickListener onclickListener) {
        super(context, R.style.clock_release_cycle_select_dialog);
        mContext = context;
        mCurrentItem = currentItem;
        mOnClickListener = onclickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clock_dialog_select_cycle);
        setCanceledOnTouchOutside(false);

        initWindowParams();
        initView();
    }

    private void initWindowParams() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (Utils.getWindowWidth(mContext) * 0.7);
        lp.height = (int) (Utils.getWindowWidth(mContext) * 0.5);
        dialogWindow.setGravity(Gravity.CENTER);
        dialogWindow.setAttributes(lp);
    }

    private void initView() {
        TextView cancel = findViewById(R.id.cycle_cancel);
        TextView save = findViewById(R.id.cycle_save);
        WheelView wheelView = findViewById(R.id.wheel_view);

        wheelView.setCyclic(false);
        wheelView.setTextColorCenter(mContext.getResources().getColor(R.color.clock_white));
        wheelView.setDividerColor(mContext.getResources().getColor(R.color.clock_white));
        final List<String> mOptionsItems = new ArrayList<>();
        mOptionsItems.add("1");
        mOptionsItems.add("2");
        mOptionsItems.add("7");
        mOptionsItems.add("15");
        wheelView.setAdapter(new ArrayWheelAdapter(mOptionsItems));
        wheelView.setCurrentItem(mCurrentItem);
        wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mSelectedCycle = mOptionsItems.get(index);
                mCurrentItem = index;
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnClickListener.save(mSelectedCycle, mCurrentItem);
                dismiss();
            }
        });
    }

    public interface IOnClickListener {
        void save(String selectedCycle, int currentItem);
    }

}
